package com.stone.business.sbqxjl.action;

/**
 * <pre>
 * 设备缺陷记录
 * @author 
 * </pre>
 */

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.gzrwd.pojo.TGzrwd;
import com.stone.business.gzrwd.pojo.TGzrwdRw;
import com.stone.business.gzrwd.service.ZbjlCommonService;
import com.stone.business.plan.pojo.TBzgzrw;
import com.stone.business.plan.pojo.TPlan;
import com.stone.business.plan.pojo.TPlanBz;
import com.stone.business.plan.service.PlanService;
import com.stone.business.sbqxjl.pojo.TSbqxjl;

import com.stone.para.IConstant;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.sys.service.DictionaryService;
import com.stone.sys.service.UserService;
import com.stone.utils.DateFormatTool;
import com.stone.utils.JsonUtils;
import com.stone.utils.MainUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.SpringUtil;
import frm.ssh.util.page.SearchBean;

public class SbqxjlAction extends BaseAction {

	private static final long serialVersionUID = -4647594073681054487L;

	private ZbjlCommonService myService;
	private PlanService planService;
	private TSbqxjl entity;
	private TSbqxjl entitytz;
	private String id1;
	private String id;
	private String qxly;
	private String bdz;
	private String bdzname;
	// 设备类型
	private List<TDictionary> sblxList;

	// 缺陷类别
	private List<TDictionary> qxlbList;
	// 缺陷来源
	private List<TDictionary> qxlyList;
	private List<TPlanBz> planbzList;

	// 调度员
	private List<TDictionary> ddyList;

	// 到位人员
	private List<TDictionary> dwryList;
	List<HashMap<String, Object>> xszqList;
	private String state;
	private List ddqxList = null;
	private List sbqxExistList = null;
	private HashMap mapdefect = null;
	Integer cid = null;
	private String isAdmin;
	// 任务列表
	private List<TGzrwdRw> listRwd = null;
	/**
	 * 进入详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {

		// 修改进入
		if (!"0".equals(id)) {

			if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon1())
					&& !super.getSearchBean().getCon1().equals("0")) {// 检修缺陷
				this.entity = (TSbqxjl) myService.findById(Long.parseLong(id),
						TSbqxjl.class.getName());
				ddqxList = myService
						.findListBySql("select a.cid as id,a.serial_no as serialno,TO_CHAR(a.resolve_time,'YYYY-MM-DD HH24:MI') as resolvetime, a.RESOLVE_CONTENT as RESOLVECONTENT,TO_CHAR(a.report_time,'YYYY-MM-DD HH24:MI') as reporttime,a.reporter as reporter,a.content as content,a.removed as remove,a.category as category,get_bdzname(a.address) as bdzname,get_bdzid(a.address) as bdzid from dm_defect a where cid = '"
								+ this.entity.getDefectid() + "'");
				mapdefect = (HashMap) ddqxList.get(0);
			} else {// 调度过来的缺陷
				sbqxExistList = myService
						.findListBySql("select id as id from TAB_SBQXJL a where defectid = '"
								+ id + "'");
				if (sbqxExistList.size() == 1) {
					HashMap map = (HashMap) sbqxExistList.get(0);
					this.entity = (TSbqxjl) myService.findById(Long
							.parseLong(map.get("id").toString()), TSbqxjl.class
							.getName());
					ddqxList = myService
							.findListBySql("select a.cid as id,a.serial_no as serialno,TO_CHAR(a.resolve_time,'YYYY-MM-DD HH24:MI') as resolvetime, a.RESOLVE_CONTENT as RESOLVECONTENT,TO_CHAR(a.report_time,'YYYY-MM-DD HH24:MI') as reporttime,a.reporter as reporter,a.content as content,a.removed as remove,a.category as category,get_bdzname(a.address) as bdzname,get_bdzid(a.address) as bdzid from dm_defect a where cid = '"
									+ this.entity.getDefectid() + "'");
					mapdefect = (HashMap) ddqxList.get(0);
				} else {
					this.entity = new TSbqxjl();
					TOrgan o = (TOrgan) super.getSession().getAttribute(
							IConstant.SESSION_TORGAN);
					if (o.getId() == 21 || o.getId() == 22 || o.getId() == 23
							|| o.getId() == 24) {
						entity.setXqbz(o.getId() + "");
						entity.setXqbzname(o.getOrganName());
					}
					ddqxList = myService
							.findListBySql("select a.cid as id,a.serial_no as serialno,TO_CHAR(a.resolve_time,'YYYY-MM-DD HH24:MI') as resolvetime, a.RESOLVE_CONTENT as RESOLVECONTENT,TO_CHAR(a.report_time,'YYYY-MM-DD HH24:MI') as reporttime,a.reporter as reporter,a.content as content,a.removed as remove,a.category as category,get_bdzid(a.address) as bdzid,get_bdzname(a.address) as bdzname from dm_defect a where cid = '"
									+ id + "'");
					mapdefect = (HashMap) ddqxList.get(0);
					entity.setQxbh((String) mapdefect.get("serialno"));
					entity.setBdz(mapdefect.get("bdzid").toString());
					entity.setBdzname((String) mapdefect.get("bdzname"));
					entity.setFxrq(DateFormatTool.formatToDate(
							(String) mapdefect.get("reporttime"), true));
					entity.setQxnr((String) mapdefect.get("content"));
					entity.setQxlb(((BigDecimal) mapdefect.get("category"))
							.intValue()
							+ "");
					// sbqxjl.setDefectid((Long)map.get("id"));
					entity.setDefectid(((BigDecimal) mapdefect.get("id"))
							.longValue());
					entity.setState("0");
					cid = ((BigDecimal) mapdefect.get("id")).intValue();

					// this.entity.setState(super.getSearchBean().getCon1());
					this.entity.setId(Long.parseLong("0"));
					this.entity.setQxly(qxly);
					/*// 记录填写人时间
					String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
					entity.setCjsj(DateFormatTool.formatToDate(sysdate, true));
					entity.setCjr(((TUser) super.getSession().getAttribute(
							IConstant.SESSION_TUSER)).getName());
					entity.setCjrid(((TUser) super.getSession().getAttribute(
							IConstant.SESSION_TUSER)).getId());*/
				}
			}

			// 检修缺陷新建进入
		} else {

		}

		// 获得字典
		getDic();

		return SUCCESS;
	}
	/**
	 * 进入设备缺陷调整详细页面
	 * @return
	 * @throws AppException
	 */
	@SuppressWarnings("unchecked")
	public String initJhsjtz() throws AppException {
		
		this.entitytz = (TSbqxjl) myService.findById(Long.parseLong(id),
				TSbqxjl.class.getName());

		return "JHSJTZ-SUCCESS";
	}
	/**
	 * 保存
	 * 
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {

		// 系统时间
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");

		// 新建时
		if (entity.getId() == 0) {

			String organCode = String.valueOf(super.getSession().getAttribute(
					IConstant.SESSION_TORGAN_CODE));

			entity.setOuid(organCode);
			// entity.setCjsj(DateFormatTool.formatToDate(sysdate, true));
			myService.save(entity);
		} else {
			// by wangtao add20110330
			if (entity.getXcsj() != null
					&& !StringUtils.isNullOrBlankFull(entity.getXcrname())
					&& !StringUtils.isNullOrBlankFull(entity.getXcyy())
					&& StringUtils.isNullOrBlankFull(entity.getCjr1())) {
				entity.setCjr1(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getName());
				entity.setCjrid1(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getId());
				entity.setCjsj1(DateFormatTool.formatToDate(sysdate, true));

			}

			entity.setXgr(((TUser) super.getSession().getAttribute(
					IConstant.SESSION_TUSER)).getName());
			entity.setXgrid(((TUser) super.getSession().getAttribute(
					IConstant.SESSION_TUSER)).getId());
			entity.setXgsj(DateFormatTool.formatToDate(sysdate, true));
			myService.update(entity);
		}

		// 获得字典
		getDic();

		super.setInfo("保存成功");
		String rtn = "";
		if (!super.getSearchBean().getCon1().equals("0")) {
			rtn = "DEL-SUCCESS";
		} else {
			rtn = "DDQX-LIST";
		}

		return rtn;
	}

	/**
	 * 查询字典项目
	 */
	public void getDic() {

		// 设备类型
		DictionaryService dicService = (DictionaryService) SpringUtil
				.getInstance().getBean("dicService");
		sblxList = dicService.findList("SBLX");

		// 缺陷类别
		qxlbList = dicService.findList("QXLB");
		// 缺陷来源
		// qxlyList = dicService.findList("QXLY");
		qxlyList = dicService
				.findListByHql(" from TDictionary where itemType = 'QXLY' and itemId = '"
						+ this.entity.getQxly() + "'");
		if (super.getSession().getAttribute(IConstant.SESSION_ISADMIN).equals(
				"1")) {
			qxlyList = dicService.findList("QXLY");
		}
		isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
		// 到位人员
		// dwryList =
		// dicService.findListByHql(" from TBzUser where usertype = (select id from TDictionary where itemType = 'BZ' and itemId = '12')");
		dwryList = myService
				.getListByDHql(
						"u.id as userid, u.name as username",
						"u.id = ur.userId and ur.roleId = r.id and r.nameen in('ZBZ','BZZ','GLRY')",
						null, "group by u.id ,u.name order by u.id",
						"TUser u, TUserRole ur , TRole r");
	}

	public void getXsjl() throws Exception {

		try {

			String bdzname = super.getRequest().getParameter("bdzname");
			// 巡视周期
			String sql = "select t.xsrq as itemid, t.xsrq as itemname from TAB_NEXTMONTH_XSJH t where to_char(t.xsrq,'yyyy-mm-dd')>=to_char(sysdate,'yyyy-mm-dd') and t.bdzname like '%"
					+ bdzname + "%'";
			UserService userServiceDIMIS = (UserService) SpringUtil
					.getInstance().getBean("userServiceDIMIS");
			xszqList = userServiceDIMIS.findListBySql(sql);
		} catch (Exception e) {
			xszqList = new ArrayList<HashMap<String, Object>>();
		}
		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
				"yyyy-MM-dd");
		JSON json = JSONSerializer.toJSON(xszqList, jsonConfig);
		super.getResponse().getWriter().print(json.toString());

	}

	/**
	 * by wangtao add20110115
	 */
	public void sbqxFileCountCheck() throws Exception {

		long count = myService.getTotalByDHql(
				"tableid = ? and colname = 'TAB_SBQXJL-QXZP'",
				new Object[] { Long.parseLong(this.id) }, "TFile");

		if (count == 0) {
			super.getResponse().getWriter().print("{count:0}");
		} else {
			super.getResponse().getWriter().print("{count:1}");
		}
	}
	/**
	 * by wangtao add20120711
	 */
	public void jhxqrqCheck() throws Exception {
		String roles = (String) super.getSession().getAttribute(IConstant.SESSION_ROLES);
		// 计划消缺日期不能大于缺陷发生日期一年
		String sql = "select case when ceil(t.jhxcjssj-t.fxrq)>365 then '1' else '0' end as flag from tab_sbqxjl t where t.id= '"
				+ Long.parseLong(this.id) + "'";
		List qxList = myService
		.findListBySql(sql);
		HashMap map = (HashMap) qxList.get(0);
		String flag=map.get("flag").toString();
		long count=Long.parseLong(flag);
		 if (roles.contains("gqgl")||roles.contains("isadmin")) {
			 count=0;
		 }
		if (count == 0) {
			super.getResponse().getWriter().print("{count:0}");
		} else {
			super.getResponse().getWriter().print("{count:1}");
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws AppException
	 */
	public String delete() throws AppException {

		myService.deleteSbqxjl(id);

		return "DEL-SUCCESS";

	}

	/**
	 * pass 流转
	 * 
	 * @return
	 * @throws AppException
	 */
	public String history() throws AppException {
		// 状态修改
		entity.setState(state);
		if (state.equals("1")) {
			// by wangtao add20120720
			// 系统时间
			String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
			if (StringUtils.isNullOrBlankFull(entity.getCjr())) {
				entity.setCjr(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getName());
				entity.setCjrid(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getId());
				entity.setCjsj(DateFormatTool.formatToDate(sysdate, true));

			}
			
			// 新建时
			if (entity.getId() == 0) {
			     myService.save(entity);
			}else{
				myService.update(entity);
			}

			TPlan tplan = new TPlan();
			tplan.setStationid(entity.getBdz());
			tplan.setStationname(entity.getBdzname());
			tplan.setFzr(entity.getHbr());
			tplan.setResponsible(entity.getHbrname());
			tplan.setStartdate(entity.getJhxcsj());
			tplan.setEnddate(entity.getJhxcjssj());
			tplan.setWorkcontent(entity.getQxnr());
			tplan.setStatus("9");
			tplan.setPlantype("1");
			myService.save(tplan);
			// 消缺班组
			if (entity.getXqbz() != null) {
				String[] indexs = entity.getXqbz().split(",");
				String[] indexsbz = entity.getXqbzname().split(",");
				this.planbzList = new ArrayList<TPlanBz>();
				int i = 0;
				for (i = 0; i < indexs.length; i++) {
					TPlanBz bz = new TPlanBz();
					bz.setId(Long.parseLong("0"));
					bz.setDelflag("0");
					bz.setBzid(Long.parseLong(indexs[i]));
					bz.setBzname(indexsbz[i]);
					bz.setFzrid(entity.getHbr());
					bz.setFzrname(entity.getHbrname());
					bz.setCyid(entity.getDyfxr());//计划消缺成员id
					bz.setCyname(entity.getJsrname());//计划消缺成员
					this.planbzList.add(bz);
					bz.setPlanid(tplan.getId());
					bz.setGzrw(entity.getQxnr());
					myService.save(bz);
					// by wangtao add20120502
					TBzgzrw bzgzrw = new TBzgzrw();
					bzgzrw.setId(0L);
					bzgzrw.setBzid(Long.parseLong(indexs[i]));
					bzgzrw.setPlanid(tplan.getId());
					bzgzrw.setGznr(entity.getQxnr());
					bzgzrw.setSbid(entity.getSbid());
					bzgzrw.setSblx(entity.getSblx());
					bzgzrw.setSbqxid(entity.getId());   // 设备缺陷id
					bzgzrw.setSbname(entity.getQxwz()); // 设备名称
					myService.save(bzgzrw);
				}
         	myService
					.insertBySql("update dm_defect set flag = '1' where cid = '"
							+ entity.getDefectid() + "'");
			}
		}else{
			myService.update(entity);
		}
		
		// 获得字典
		getDic();
		String info = "";
		if (state.equals("1")) {
			info = "已送消缺计划";
		} else if (state.equals("2")) {
			info = "送已消除";
		} else {
			info = "归档成功";
		}
		super.setInfo(info);

		return SUCCESS;
	}
	/**
	 * 调整计划消缺时间并生成工作计划
	 * 
	 * @return
	 * @throws AppException
	 */
	public String saveJhsjtz() throws AppException {
		this.entity = (TSbqxjl) myService.findById(entitytz.getId(),
				TSbqxjl.class.getName());
		this.entity.setTzjhxcsj(this.entitytz.getTzjhxcsj());
		this.entity.setTzjhxcjssj(entitytz.getTzjhxcjssj());
			myService.update(entity);
			TPlan tplan = new TPlan();
			tplan.setStationid(entity.getBdz());
			tplan.setStationname(entity.getBdzname());
			tplan.setFzr(entity.getHbr());
			tplan.setResponsible(entity.getHbrname());
			tplan.setStartdate(entity.getTzjhxcsj());//调整计划消缺开始时间
			tplan.setEnddate(entity.getTzjhxcjssj());
			tplan.setWorkcontent(entity.getQxnr());
			tplan.setStatus("9");
			tplan.setPlantype("1");
			myService.save(tplan);
			// 消缺班组
			if (entity.getXqbz() != null) {
				String[] indexs = entity.getXqbz().split(",");
				String[] indexsbz = entity.getXqbzname().split(",");
				this.planbzList = new ArrayList<TPlanBz>();
				int i = 0;
				for (i = 0; i < indexs.length; i++) {
					TPlanBz bz = new TPlanBz();
					bz.setId(Long.parseLong("0"));
					bz.setDelflag("0");
					bz.setBzid(Long.parseLong(indexs[i]));
					bz.setBzname(indexsbz[i]);
					bz.setFzrid(entity.getHbr());
					bz.setFzrname(entity.getHbrname());
					bz.setCyid(entity.getDyfxr());//计划消缺成员id
					bz.setCyname(entity.getJsrname());//计划消缺成员
					this.planbzList.add(bz);
					bz.setPlanid(tplan.getId());
					bz.setGzrw(entity.getQxnr());
					myService.save(bz);
					// by wangtao add20120502
					TBzgzrw bzgzrw = new TBzgzrw();
					bzgzrw.setId(0L);
					bzgzrw.setBzid(Long.parseLong(indexs[i]));
					bzgzrw.setPlanid(tplan.getId());
					bzgzrw.setGznr(entity.getQxnr());
					bzgzrw.setSbid(entity.getSbid());
					bzgzrw.setSblx(entity.getSblx());
					bzgzrw.setSbqxid(entity.getId());   // 设备缺陷id
					bzgzrw.setSbname(entity.getQxwz()); // 设备名称
					myService.save(bzgzrw);
				}
			}
			super.setInfo("计划消缺时间已经调整并生成了新的工作计划");
		return "JHSJTZ-SUCCESS";
	}
	/**
	 * pass 流转 设备缺陷—>工作计划->任务单生成
	 * 
	 * @return
	 * @throws AppException
	 */
	public String historyrwd() throws AppException {
		// 状态修改
		/*System.out.println(state);
		if(null==state){
			state="1";
		}*/
		entity.setState(state);
		if (state.equals("1")) {
			// by wangtao add20120720
			// 系统时间
			String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
			if (StringUtils.isNullOrBlankFull(entity.getCjr())) {
				entity.setCjr(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getName());
				entity.setCjrid(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getId());
				entity.setCjsj(DateFormatTool.formatToDate(sysdate, true));

			}
			
			// 新建时
			if (entity.getId() == 0) {
			     myService.save(entity);
			}else{
				myService.update(entity);
			}
			//返回设备缺陷ID by wangtao add20121121
			try {
				super.getResponse().getWriter().print("{sbqxid:"+entity.getId()+"}");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			TPlan tplan = new TPlan();
			tplan.setStationid(entity.getBdz());
			tplan.setStationname(entity.getBdzname());
			tplan.setFzr(entity.getHbr());
			tplan.setResponsible(entity.getHbrname());
			tplan.setStartdate(entity.getJhxcsj());
			tplan.setEnddate(entity.getJhxcjssj());
			tplan.setWorkcontent(entity.getQxnr());
			tplan.setStatus("9");
			tplan.setPlantype("1");
			myService.save(tplan);
			// 消缺班组
			if (entity.getXqbz() != null) {
				String[] indexs = entity.getXqbz().split(",");
				String[] indexsbz = entity.getXqbzname().split(",");
				this.planbzList = new ArrayList<TPlanBz>();
				int i = 0;
				for (i = 0; i < indexs.length; i++) {
					TPlanBz bz = new TPlanBz();
					bz.setId(Long.parseLong("0"));
					bz.setDelflag("0");
					bz.setBzid(Long.parseLong(indexs[i]));
					bz.setBzname(indexsbz[i]);
					bz.setFzrid(entity.getHbr());
					bz.setFzrname(entity.getHbrname());
					bz.setCyid(entity.getDyfxr());//计划消缺成员id
					bz.setCyname(entity.getJsrname());//计划消缺成员
					this.planbzList.add(bz);
					bz.setPlanid(tplan.getId());
					bz.setGzrw(entity.getQxnr());
					myService.save(bz);
					// by wangtao add20120502
					TBzgzrw bzgzrw = new TBzgzrw();
					bzgzrw.setId(0L);
					bzgzrw.setBzid(Long.parseLong(indexs[i]));
					bzgzrw.setPlanid(tplan.getId());
					bzgzrw.setGznr(entity.getQxnr());
					bzgzrw.setSbid(entity.getSbid());
					bzgzrw.setSblx(entity.getSblx());
					bzgzrw.setSbqxid(entity.getId());   // 设备缺陷id
					bzgzrw.setSbname(entity.getQxwz()); // 设备名称
					myService.save(bzgzrw);
				}
         //设备缺陷—>工作计划->任务单生成
				listRwd = new ArrayList<TGzrwdRw>();

				TGzrwd gzrwd = new TGzrwd();
				gzrwd.setId(Long.parseLong("0"));
				gzrwd.setState(super.getSearchBean().getCon1());
				//TGzrwd.setState("0");
				gzrwd.setPlanid(tplan.getId());
				
				//TPlan tplan = (TPlan)myService.findById(tplan.getId()(), TPlan.class.getName());
				gzrwd.setBdz(tplan.getStationid());
				gzrwd.setBdzname(tplan.getStationname());
				gzrwd.setDwry(tplan.getDwry());
				gzrwd.setDwryname(tplan.getDwryname());
				gzrwd.setCzr(tplan.getYsr());
				gzrwd.setCzrname(tplan.getYsrname());
				gzrwd.setBdzname(tplan.getStationname());
				gzrwd.setZbz(tplan.getFzr());
				gzrwd.setZbzname(tplan.getResponsible());
				String bzid=((TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getId().toString();
				if(bzid.equals("21")||bzid.equals("22")||bzid.equals("23")||bzid.equals("24")){//各班组起草工单
					//TPlanBz tplanbz = (TPlanBz)myService.findById(bzid, TPlanBz.class.getName());
					List bzlist = myService.findListByHql("from TPlanBz where delflag='0' and bzid =" + bzid+" and planid="+tplan.getId());
					if(bzlist.size()!=0){
					TPlanBz tplanbz =(TPlanBz)bzlist.get(0);
					gzrwd.setBgzr(bzid);//起草任务单班组
					gzrwd.setZbyname(tplanbz.getCyname());
					gzrwd.setZby(tplanbz.getCyid());	
					gzrwd.setZbz(tplanbz.getFzrid());
					gzrwd.setZbzname(tplanbz.getFzrname());
					List<TBzgzrw> bzgzrwList = myService.findListByHql("from TBzgzrw where bzid =" + bzid+" and planid="+tplan.getId());	
					for (int j =0;j<bzgzrwList.size();j++) {
								TBzgzrw tbzgzrw = bzgzrwList.get(j);
								TGzrwdRw rw = new TGzrwdRw();
								// 将班组工作任务复制给工作任务单任务
								MainUtils.copy(tbzgzrw, rw);
								rw.setId(Long.parseLong("0"));
								rw.setBzid(tbzgzrw.getBzid());
								rw.setGzrwdid(entity.getId());
								listRwd.add(rw);
					}
					}
					
				}else{//多班组专工起草工单
					//TPlanBz tplanbz = (TPlanBz)myService.findById(bzid, TPlanBz.class.getName());
					List bzlist = myService.findListByHql("from TPlanBz where delflag='0' and planid="+tplan.getId());
					for (int j =0;j<bzlist.size();j++) {
					TPlanBz tplanbz =(TPlanBz)bzlist.get(j);
					//TGzrwd.setBgzr(bzid);//起草任务单班组
					//TGzrwd.setZbyname(tplanbz.getCyname());
					//TGzrwd.setZby(tplanbz.getCyid());	
					List<TBzgzrw> bzgzrwList = myService.findListByHql("from TBzgzrw where bzid =" + tplanbz.getBzid()+" and planid="+tplan.getId());	
					for (int k =0;k<bzgzrwList.size();k++) {
						TBzgzrw tbzgzrw = bzgzrwList.get(k);
						TGzrwdRw rw = new TGzrwdRw();
						// 将班组工作任务复制给工作任务单任务
						MainUtils.copy(tbzgzrw, rw);
						rw.setBzid(tbzgzrw.getBzid());
						rw.setId(Long.parseLong("0"));
						rw.setGzrwdid(entity.getId());
						listRwd.add(rw);
					}
								
				  }
				}
				
				gzrwd.setYcclqk(tplan.getPowerrange());
				gzrwd.setCzkssj(tplan.getStartdate());
				gzrwd.setCzjssj(tplan.getEnddate());
				// 负责人
				//zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'BZZ')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
				//zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'group'or r.nameen = 'bhbby' or r.nameen = 'jxbby' or r.nameen = 'byqby' or r.nameen = 'sybby')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
				String organid = ((TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getId().toString();
				
						  //by wangtao update20110319
				gzrwd.setOuid(((TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getOrganCode().toString());
				super.getRequest().setAttribute("GZRWDLIST", new ArrayList<HashMap<String, Object>>());
				
				gzrwd.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
				gzrwd.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
				
				// by wangtao update20110105
				 sysdate = myService.getSysdate("yyyyMMdd");
				List<HashMap<String, Object>> list = myService.findListBySql("select MAX(bh) as bh from TAB_GZRWD where bh like '" + sysdate + "%'");
				
				// 票号取得
				if (list.get(0).get("bh") == null || "".equals(list.get(0).get("bh"))) {
					gzrwd.setBh(sysdate + "0001");
					
				} else {
					if(gzrwd.getBh()==null||gzrwd.getBh().equals("")){
						gzrwd.setBh(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
				
					}
				
				}
				
				gzrwd.setRq(DateFormatTool.dateFormatDate(tplan.getStartdate(),false));
				
				myService.save(gzrwd);
				myService.savePlanMonth(gzrwd.getId(),this.listRwd);
			}
			
				////////////////////////////////////
		

			myService
					.insertBySql("update dm_defect set flag = '1' where cid = '"
							+ entity.getDefectid() + "'");
		}else{
			myService.update(entity);
		}
		
		// 获得字典
		getDic();
		String info = "";
		if (state.equals("1")) {
			info = "已送消缺计划";
		} else if (state.equals("2")) {
			info = "送已消除";
		} else {
			info = "归档成功";
		}
		super.setInfo(info);

		return SUCCESS;
	}
	/**
	 * 驳回
	 * 
	 * @return
	 * @throws AppException
	 */
	public String reject() throws AppException {

		// 状态修改
		entity.setState(state);
		myService.update(entity);
		// 获得字典
		getDic();

		super.setInfo("驳回成功");

		return SUCCESS;
	}
	/**
	 * 手动生成月度计划
	 * 
	 * @return
	 * @throws AppException
	 */
	public String savePlan() throws AppException {
	
			TPlan tplan = new TPlan();
			tplan.setStationid(entity.getBdz());
			tplan.setStationname(entity.getBdzname());
			tplan.setFzr(entity.getHbr());
			tplan.setResponsible(entity.getHbrname());
			tplan.setStartdate(entity.getJhxcsj());
			tplan.setEnddate(entity.getJhxcjssj());
			tplan.setWorkcontent(entity.getQxnr());
			tplan.setStatus("9");
			tplan.setPlantype("1");
			myService.save(tplan);
			// 消缺班组
			if (entity.getXqbz() != null) {
				String[] indexs = entity.getXqbz().split(",");
				String[] indexsbz = entity.getXqbzname().split(",");
				this.planbzList = new ArrayList<TPlanBz>();
				int i = 0;
				for (i = 0; i < indexs.length; i++) {
					TPlanBz bz = new TPlanBz();
					bz.setId(Long.parseLong("0"));
					bz.setDelflag("0");
					bz.setBzid(Long.parseLong(indexs[i]));
					bz.setBzname(indexsbz[i]);
					bz.setFzrid(entity.getHbr());
					bz.setFzrname(entity.getHbrname());
					this.planbzList.add(bz);
					bz.setPlanid(tplan.getId());
					bz.setGzrw(entity.getQxnr());
					myService.save(bz);
					// by wangtao add20120502
					TBzgzrw bzgzrw = new TBzgzrw();
					bzgzrw.setId(0L);
					bzgzrw.setBzid(Long.parseLong(indexs[i]));
					bzgzrw.setPlanid(tplan.getId());
					bzgzrw.setGznr(entity.getQxnr());
					bzgzrw.setSbid(entity.getSbid());
					bzgzrw.setSblx(entity.getSblx());
					bzgzrw.setSbqxid(entity.getId());   // 设备缺陷id
					bzgzrw.setSbname(entity.getQxwz()); // 设备名称
					myService.save(bzgzrw);
				}

			}

		// 获得字典
		getDic();
		String info = "月度计划创建成功";
		super.setInfo(info);

		return SUCCESS;
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMyService(ZbjlCommonService myService) {
		this.myService = myService;
	}

	public TSbqxjl getEntity() {
		return entity;
	}

	public void setEntity(TSbqxjl entity) {
		this.entity = entity;
	}

	public List<TDictionary> getSblxList() {
		return sblxList;
	}

	public List<TDictionary> getQxlbList() {
		return qxlbList;
	}

	public List<TDictionary> getDdyList() {
		return ddyList;
	}

	public List<TDictionary> getDwryList() {
		return dwryList;
	}

	public List<TDictionary> getQxlyList() {
		return qxlyList;
	}

	public String getQxly() {
		return qxly;
	}

	public void setQxly(String qxly) {
		this.qxly = qxly;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public List getDdqxList() {
		return ddqxList;
	}

	public void setDdqxList(List ddqxList) {
		this.ddqxList = ddqxList;
	}

	public HashMap getMapdefect() {
		return mapdefect;
	}

	public void setMapdefect(HashMap mapdefect) {
		this.mapdefect = mapdefect;
	}

	public String getId1() {
		return id1;
	}

	public void setId1(String id1) {
		this.id1 = id1;
	}

	public Integer getCid() {
		return cid;
	}

	public void setCid(Integer cid) {
		this.cid = cid;
	}

	public List<HashMap<String, Object>> getXszqList() {
		return xszqList;
	}

	public void setXszqList(List<HashMap<String, Object>> xszqList) {
		this.xszqList = xszqList;
	}

	public List getSbqxExistList() {
		return sbqxExistList;
	}

	public void setSbqxExistList(List sbqxExistList) {
		this.sbqxExistList = sbqxExistList;
	}

	public String getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(String isAdmin) {
		this.isAdmin = isAdmin;
	}

	public List<TGzrwdRw> getListRwd() {
		return listRwd;
	}

	public void setListRwd(List<TGzrwdRw> listRwd) {
		this.listRwd = listRwd;
	}
	public TSbqxjl getEntitytz() {
		return entitytz;
	}
	public void setEntitytz(TSbqxjl entitytz) {
		this.entitytz = entitytz;
	}

}
